package CalendarComponents;

import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.LinkedList;

import javax.swing.*;

import Managers.*;
import calendar.GlobalSettings;
import calendar.MainWindow;

public class MenuBar implements ICatObserver {
    JTextArea text;
    JScrollPane scrollPane;
    JMenuItem day, week, month;
    JMenu filter;  
    JMenuBar menuBar;

    final LinkedList<JCheckBoxMenuItem> lit = new LinkedList<JCheckBoxMenuItem>();
    

    public JMenuBar createMenuBar()
    {
    	CategoryManager.getInstance().addObserver(this);
        JMenu menu, settings;
        JMenuItem addedit, changeview, deselect;
		final JMenuItem acb, cbcb, cccb;
        
        menuBar = new JMenuBar();

        menu = new JMenu("Menu");
        addedit = new JMenuItem("Add Appointments");
        addedit.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_A, ActionEvent.CTRL_MASK));
        ApptListener al = new ApptListener();
        addedit.addActionListener(al);
        menu.add(addedit);
        
        ViewListener vl = new ViewListener();
        changeview = new JMenu("Change view...");
        day = new JMenuItem("Day View");
        day.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_D, ActionEvent.CTRL_MASK));
        day.addActionListener(vl);
        
        changeview.add(day);
        week = new JMenuItem("Week View");
        week.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_W, ActionEvent.CTRL_MASK));
        week.addActionListener(vl);
        
        changeview.add(week);
        month = new JMenuItem("Month View");
        month.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_M, ActionEvent.CTRL_MASK));
        month.addActionListener(vl);
        
        
        
        changeview.add(month);
        menu.add(changeview);
        menuBar.add(menu);
		
		filter = new JMenu("Filter");
		CategoryManager.getInstance();
		for(Category c : CategoryManager.getList())
		{    
			JCheckBoxMenuItem cb1 = new JCheckBoxMenuItem(c.getName());
			cb1.setBackground(c.getColor());
			if(GlobalSettings.color_contrast)
			{
				Color op = new Color(0xFFFFFFFF - c.getColor().getRGB());
				cb1.setForeground(op);
			}
			cb1.setBorder(BorderFactory.createLineBorder(c.getColor()));
			cb1.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e)
				{
					LinkedList<Category> ll = new LinkedList<Category>();
					Category[] cats;
					for(JCheckBoxMenuItem i : lit)
					{
						if(i.isSelected())
						{
							
							ll.add(CategoryManager.getCategory(i.getText()));
						}
					}
					
					cats = ll.toArray(new Category[]{});
					
					CalendarManager.getInstance().notifyObservers(cats);
				}
				
			});
			filter.add(cb1);
			lit.add(cb1);
		}
		
		filter.addSeparator();
		
		deselect = new JMenuItem("Deselect all...");
		deselect.addActionListener( new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				for(JCheckBoxMenuItem i : lit)
				{
					i.setSelected(false);
				}
				CalendarManager.getInstance().notifyObservers();
			}
			
		});
		filter.add(deselect);
	    menuBar.add(filter);
	
	    settings = new JMenu("Settings");

	    acb = new JCheckBoxMenuItem("Audio Feedback");	
        acb.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_F1, 0));
		acb.addItemListener(new ItemListener()
		{

			@Override
			public void itemStateChanged(ItemEvent arg0) {
				GlobalSettings.audio_mode = !GlobalSettings.audio_mode;
			}
			
		});
		settings.add(acb);
		
	    cccb = new JCheckBoxMenuItem("Color Contrasting");	
        cccb.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_F2, 0));
	    cccb.addItemListener(new ItemListener(){

			@Override
			public void itemStateChanged(ItemEvent arg0) {
				GlobalSettings.color_contrast = !GlobalSettings.color_contrast;
				updateFilterLooks();
			}
			
		});
		settings.add(cccb);
	
		
	    cbcb = new JCheckBoxMenuItem("Color Blind Mode");
        cbcb.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_F3, 0));
		cbcb.addItemListener(new ItemListener(){

			@Override
			public void itemStateChanged(ItemEvent arg0) {
				GlobalSettings.color_blind_mode = !GlobalSettings.color_blind_mode;
				CalendarManager.getInstance().notifyObservers();
			}
			
		});
		settings.add(cbcb);
		
		
		menuBar.add(settings);
		menuBar.add(Box.createHorizontalGlue());
		menuBar.add(new Clock());

	    return menuBar;
	}
    
    //add new categories to the filter.
    public void updateFilter(Category c)
    {
		JCheckBoxMenuItem cb1 = new JCheckBoxMenuItem(c.getName());
		cb1.setBackground(c.getColor());
		
		if(GlobalSettings.color_contrast)
		{
			Color op = new Color(0xFFFFFFFF - c.getColor().getRGB());
			cb1.setForeground(op);
		}
		
		cb1.setBorder(BorderFactory.createLineBorder(c.getColor()));
		
		cb1.addActionListener(new ActionListener()
		{
	
			@Override
			public void actionPerformed(ActionEvent e)
			{
				LinkedList<Category> ll = new LinkedList<Category>();
				Category[] cats;
				for(JCheckBoxMenuItem i : lit)
				{
					
					if(i.isSelected())
					{
						
						ll.add(CategoryManager.getCategory(i.getText()));
					}
				}
				
				cats = ll.toArray(new Category[]{});
				CalendarManager.getInstance().notifyObservers(cats);
			}
			
		});
		
		filter.add(cb1,lit.size()-1);
		
		lit.add(cb1);
		
		menuBar.revalidate();
		menuBar.repaint();
		
	}

    //apply contrast settings or unapply them.
    public void updateFilterLooks()
    {
    	for(JCheckBoxMenuItem i : lit)
		{
			if(GlobalSettings.color_contrast)
			{
				i.setForeground(new Color(0xFFFFFFFF - i.getBackground().getRGB()));
			}
			else
			{
				i.setForeground(Color.BLACK);
			}
			
		}
		menuBar.revalidate();
		menuBar.repaint();
		
	}
    
	private class ViewListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
    		Calendar c = CalendarManager.getDay().getCalendar();
    		
			if(e.getSource().equals(day))
			{
				MainWindow.getTabPane().setSelectedIndex(0);
			}
			else if(e.getSource().equals(week))
			{
				MainWindow.getTabPane().setSelectedIndex(1);
			}
			else if(e.getSource().equals(month))
			{
				MainWindow.getTabPane().setSelectedIndex(2);
				MainWindow.getMonthsTabPane().setSelectedIndex(c.get(Calendar.MONTH));
			}
		}
	}
	
	private class ApptListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			//create Properties dialog
			PropertiesDialog pd = new PropertiesDialog(MainWindow.getMainWindow(), CalendarManager.getDay());
			//show it
			pd.setVisible(true);
			//get the appointment
			Appointment er = pd.getAppointment();
			if(er == null)
				er = Appointment.None();
			//add the appointment
			CalendarManager.AddAppointment(er);
		}
	}
	    
	@SuppressWarnings("serial")
	class AboutDialog extends JDialog implements ActionListener {
		public AboutDialog(JFrame parent, String title) {
			parent.setTitle(title);
		
			JPanel messagePane1 = new JPanel();
		 	messagePane1.setLayout(new BoxLayout(messagePane1, BoxLayout.Y_AXIS));
		 	JTextField text1 = new JTextField("Keyword");
		 	messagePane1.add(text1,BorderLayout.NORTH);
		 	JTextField text2 = new JTextField("Date");
		 	messagePane1.add(text2,BorderLayout.SOUTH);
		
		 	JPanel buttonPane = new JPanel();
		 	JButton button1 = new JButton("Next Item"); 
		 	buttonPane.add(button1); 
		 	button1.addActionListener(this);
		 	JButton button2 = new JButton("Cancel"); 
		   	buttonPane.add(button2); 
		   	button2.addActionListener(this);
		   	getContentPane().add(messagePane1, BorderLayout.NORTH);
		   	getContentPane().add(buttonPane, BorderLayout.SOUTH);
		  	setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		  	pack(); 
		   	setSize(400, 200);
		   	setVisible(true);
		}
	    	
		public void actionPerformed(ActionEvent e) {
			setVisible(false); 
			dispose(); 
		}
	}

	@Override
	public void Update(Category c) {
		updateFilter(c);		
	}
}
